package com.dailyblue.project.estate.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.project.estate.commons.bean.EstateManager;
import com.dailyblue.project.estate.commons.handler.EstateManagerException;
import com.dailyblue.project.estate.commons.mapper.EstateManagerMapper;
import com.dailyblue.project.estate.commons.util.JsonResult;
import com.dailyblue.project.estate.commons.util.ResultTool;
import com.dailyblue.project.estate.service.ManagerService;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther:Liu
 * @Testname:ManagerServiceImpl
 * @Date:2025/9/18 15:42
 */
@Service
public class ManagerServiceImpl extends ServiceImpl<EstateManagerMapper, EstateManager> implements ManagerService {
    @Resource
    private EstateManagerMapper estateManagerMapper;
    @Override
    public JsonResult saveManager(EstateManager estateManager) {
        boolean b = save(estateManager);
        if (!b) {
            throw new EstateManagerException("添加失败");
        }
        return ResultTool.success("save manager");
    }

    @Override
    public JsonResult findOne(String id) {
        EstateManager estateManager = getById(id);
        return ResultTool.success(estateManager);
    }

    @Cacheable(cacheNames = "AllEstateManagers")
    public JsonResult findAll() {
        List<EstateManager> list = list();
        return ResultTool.success(list);
    }

    @CacheEvict(cacheNames = "AllEstateManagers",allEntries = true)
    public JsonResult deleteOne(String id) {
        boolean b = removeById(id);
        if (!b) {
            throw new EstateManagerException("删除失败");
        }
        return ResultTool.success("delete manager");
    }

    @CacheEvict(cacheNames = "AllEstateManagers",allEntries = true)
    public JsonResult updateOne(EstateManager estateManager) {
        UpdateWrapper<EstateManager> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", estateManager.getId());
        wrapper.set("name", estateManager.getName());
        wrapper.set("community_id", estateManager.getCommunityId());
        wrapper.set("community_name", estateManager.getCommunityName());
        wrapper.set("login_name", estateManager.getLoginName());
        wrapper.set("password", estateManager.getPassword());
        wrapper.set("telephone", estateManager.getTelephone());
        wrapper.set("email", estateManager.getEmail());
        wrapper.set("role_id", estateManager.getRoleId());
        int update = estateManagerMapper.update(wrapper);
        if (update != 1) {
            throw new EstateManagerException("更新失败");
        }
        return ResultTool.success("update manager");
    }
}
